﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Portal.Cms.Model;
using Portal.Web.Model;
using Portal.Cms.Bussiness;
using Portal.Web.Utility;
using Portal.Cms.Utility;
using Portal.Core;

namespace Portal.Cms.Render
{
    public class MoveGroupDataFolderRenderBll
    {
        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut Draw(string SiteId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                string IconRoot = WebConfig.GetButtonIconRoot(OSiteParam);
                string GroupDataId = (string)WebEnvironments.Request(SiteId, "gid");
                if (string.IsNullOrEmpty(GroupDataId)) throw new Exception("Tham số không hợp lệ!");
                GroupDataCls OGroupData = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataModel(null, SiteId, GroupDataId);
                if (OGroupData == null) throw new Exception("Tham số không hợp lệ!");
                SystemBussinessUtility.CreateSystemsBussinessProcess(SiteId).CheckPermission(SiteId, GroupDataPermission.StaticId, GroupDataPermission.funcAccess, GroupDataId, WebSessionUtility.GetCurrentLoginUser().UserId, true);
                string MoveGroupDataFolderId=(string)WebEnvironments.Request(SiteId,"MoveGroupDataFolderId");
                if(string.IsNullOrEmpty(MoveGroupDataFolderId))
                {
                    throw new Exception("Tham số không hợp lệ!");
                }
                GroupDataFolderCls OMoveGroupDataFolder=CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).CreateGroupDataFolderModel(null,SiteId,MoveGroupDataFolderId);
                if (OMoveGroupDataFolder == null)
                {
                    throw new Exception("Tham số không hợp lệ!");
                }

                GroupDataFolderCls[] GroupDataFolders = CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, GroupDataId, null);
                string Html =
                              "<input id=\"txtGroupDataId\" type=\"hidden\" value=\""+GroupDataId+"\">\r\n"+
                              "<script language=javascript>\r\n"+
                              "     function ClientExecuteMove()\r\n" +
                              "     {\r\n" +
                              "         SiteId='" + SiteId + "';\r\n" +
                              "         var SelectedNode = $(\"#tree\").dynatree(\"getActiveNode\");\r\n" +
                              "         if(SelectedNode==null)\r\n" +
                              "         {\r\n" +
                              "             alert('Chưa chọn vào thư mục cần xóa!');\r\n"+
                              "             return;\r\n"+
                              "         }\r\n"+
                              "         GroupDataId=document.getElementById('txtGroupDataId').value;\r\n" +
                              "         MoveGroupDataFolderId=document.getElementById('txtMoveGroupDataFolderId').value;\r\n" +
                              "         GroupDataFolderId=SelectedNode.data.href;\r\n" +
                              "         if(confirm('Bạn đã chắc chắn chuyển chưa?')==false)return;\r\n"+
                              "         AjaxOut = Portal.Cms.Render.MoveGroupDataFolderRenderBll.ServerSideMoveGroupDataFolder(SiteId, GroupDataId, MoveGroupDataFolderId, GroupDataFolderId).value;\r\n" +
                              "         if(AjaxOut.Error)\r\n" +
                              "         {\r\n" +
                              "             alert(AjaxOut.InfoMessage);\r\n" +
                              "             return;\r\n" +
                              "         }\r\n" +
                              "         window.open('GroupDataFolder.aspx?gid='+GroupDataId,'_self');\r\n"+
                              "     }\r\n" +
                              "</script>\r\n"+
                              "<input type=hidden id=txtGroupDataId value=\"" + GroupDataId + "\">\r\n" +
                              "<input type=hidden id=txtMoveGroupDataFolderId value=\""+MoveGroupDataFolderId+"\">\r\n" +
                              "<div class=FormFunction id=divListForm>\r\n" +
                              " <h3>"+OGroupData.GroupDataName+"</h3>\r\n"+
                              " <div class=DatabaseFunction>\r\n" +
                              "        <ul>\r\n" +
                              "            <li onclick=\"javascript:ClientExecuteMove();\"><img class=image src=\"" + IconRoot + "/Move.png\"><div>Thực hiện chuyển</div></li>\r\n" +
                              "            <li onclick=\"javascript:window.open('GroupDataFolder.aspx?gid="+GroupDataId+"','_self');\"><img class=image src=\"" + IconRoot + "/back.png\"><div>Quay lại</div></li>\r\n" +
                              "        </ul>\r\n" +
                              "  </div>\r\n" +
                              

                              "<div class=TreeHolder>\r\n" +
                              "<div id=\"tree\">\r\n" +
                              "<div class=CssMoveInfo>Thư mục sẽ chuyển ["+OMoveGroupDataFolder.FolderName+"]</div>\r\n"+
                              "<ul>\r\n" +
                              " <li class=\"folder expanded\">Thư mục gốc</a>\r\n" +
                              "     <ul>\r\n";
                    for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                    {
                        if (!GroupDataFolders[iIndex].GroupDataFolderId.Equals(MoveGroupDataFolderId))
                        {
                            Html +=
                                    "<li class=\"folder expanded\"><a href=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\" target=\"_self\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                            BuildSubTreeView(SiteId, GroupDataId, MoveGroupDataFolderId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                                    "</li>\r\n";
                        }
                    }

                    Html += "</ul>\r\n" +
                            "</li>\r\n" +
                            "</ul>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n" +
                            "</div>\r\n";
                
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }

        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut BuildSubTreeView(
            string SiteId,
            string GroupDataId, 
            string MoveGroupDataFolderId,
            string ParentGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                string Html = "";
                GroupDataFolderCls[] GroupDataFolders =
                    CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).ReadingGroupDataFolder(null, SiteId, GroupDataId, ParentGroupDataFolderId);
                Html+=
                    "<ul>\r\n";
                for (int iIndex = 0; iIndex < GroupDataFolders.Length; iIndex++)
                {
                    if (!GroupDataFolders[iIndex].GroupDataFolderId.Equals(MoveGroupDataFolderId))
                    {
                        Html +=
                                "<li class=\"folder expanded\"><a target=\"_self\" href=\"" + GroupDataFolders[iIndex].GroupDataFolderId + "\">" + GroupDataFolders[iIndex].FolderName + "</a>\r\n" +
                                    BuildSubTreeView(SiteId, GroupDataId, MoveGroupDataFolderId, GroupDataFolders[iIndex].GroupDataFolderId).HtmlContent +
                                "</li>\r\n";
                    }
                }

                Html += "</ul>\r\n";
                OAjaxOut.HtmlContent = Html;
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }


        [AjaxPro.AjaxMethod(AjaxPro.HttpSessionStateRequirement.ReadWrite)]
        public static AjaxOut ServerSideMoveGroupDataFolder(
            string SiteId,
            string GroupDataId,
            string MoveGroupDataFolderId,
            string ToGroupDataFolderId)
        {
            AjaxOut OAjaxOut = new AjaxOut();
            try
            {
                SiteParam OSiteParam = WebEnvironments.CreateSiteParam(SiteId);
                CmsUtility.CreateCmsBussinessProcess(SiteId).CreateCmsProcess(SiteId).MoveGroupDataFolder(
                    null, 
                    SiteId, 
                    GroupDataId, 
                    MoveGroupDataFolderId, 
                    ToGroupDataFolderId);
            }
            catch (Exception ex)
            {
                OAjaxOut.Error = true;
                OAjaxOut.InfoMessage = ex.Message.ToString();
                OAjaxOut.HtmlContent = ex.Message.ToString();
            }
            return OAjaxOut;
        }
    }
}
